package com.vaadin.pekka.treegridwidget;

import com.vaadin.Application;
import com.vaadin.ui.Window;

public class TreegridwidgetApplication extends Application {
    @Override
    public void init() {
        Window mainWindow = new Window("Treegridwidget Application");
        setMainWindow(mainWindow);

        TreeGrid treeGrid = new TreeGrid();
        mainWindow.addComponent(treeGrid);
        String[][] columnHeaders = getColumnHeaders(1 + (int) (Math.random() * 4));
        String[][] rowHeaders = getRowHeaders(1 + (int) (Math.random() * 4));
        treeGrid.setGridData(
                columnHeaders,
                rowHeaders,
                getDataByColumns(
                        columnHeaders[columnHeaders.length - 1].length - 1,
                        rowHeaders[rowHeaders.length - 1].length - 1));
    }

    private String[][] getDataByColumns(int columns, int rows) {
        String[][] data = new String[columns][rows];
        for (String[] a : data) {
            for (int i = 0; i < a.length; i++) {
                a[i] = Math.random() * 10 + "";
            }
        }
        return data;
    }

    private String[][] getRowHeaders(int levels) {
        String[][] data = new String[levels][];
        for (int i = 0; i < data.length; i++) {
            data[i] = new String[2 + (int) (Math.random() * 4)];
            data[i][0] = "ROW" + i;
            for (int j = data[i].length - 1; j > 0; j--) {
                data[i][j] = "L" + i + "R" + j;
            }
        }
        return data;
    }

    private String[][] getColumnHeaders(int levels) {
        String[][] data = new String[levels][];
        for (int i = 0; i < data.length; i++) {
            data[i] = new String[2 + (int) (Math.random() * 4)];
            data[i][0] = "COL" + i;
            for (int j = data[i].length - 1; j > 0; j--) {
                data[i][j] = "L" + i + "C" + j;
            }
        }
        return data;
    }

}
